package com.wanxi.dangdang.controller.upload;

import com.wanxi.dangdang.common.CommonResult;
import com.wanxi.dangdang.model.pojo.Role;
import com.wanxi.dangdang.service.upload.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/9/3 0003
 * @Version: 1.0
 */
@RestController
@RequestMapping("/upload")
@Api(tags = "上传图片（需要登录）")
public class UploadController {

    @Value("${domainName}")
    private String domain;


    @Autowired
    private UploadService uploadService;


    @GetMapping("/getToken")
    public CommonResult<String> getUploadToken(){
        String upToken = uploadService.getUpToken();
        return CommonResult.successData(upToken);
    }

    @PostMapping(value = "/licenseFiles", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "营业执照文件上传接口", notes = "文件上传api必须用表单方式提交")
    public CommonResult<String> licenseFiles(@ApiParam(value = "files", required = true)
                                           @RequestParam("files") MultipartFile[] files){
        String spaceFile = "license";
        return getStringCommonResult(files, spaceFile);
    }
    @PostMapping(value = "/scenicFiles", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "景区图片文件上传接口", notes = "文件上传api必须用表单方式提交")
    public CommonResult<String> scenicFiles(@ApiParam(value = "files", required = true)
                                           @RequestParam("files") MultipartFile[] files){
        String spaceFile = "scenic";
        return getStringCommonResult(files, spaceFile);
    }

    private CommonResult<String> getStringCommonResult(MultipartFile[] files, String spaceFile) {
        String url = "";
        if (files.length == 1) {
            if (files[0].getSize() > 5 * 1024 * 1024) {
                return CommonResult.failed("单个图片不能大于5M！");
            } else {
                try {
//                    url = domain +"/"+ uploadService.uploadFile(files[0], spaceFile);
                    url = uploadService.uploadFile(files[0], spaceFile);
                    //return new ResultData<String>().success(url);

                } catch (Exception e) {
                    return CommonResult.failed("图片大于5M，请重新上传！");
                }
            }
        } else {
            for (MultipartFile file : files) {
                if (ObjectUtils.isEmpty(file)) {
                    return CommonResult.failed("文件为空请重新上传!");
                } else if (file.getSize() > 5 * 1024 * 1024) {
                    return CommonResult.failed("单个图片不能大于5M！");
                } else {
                    try {
//                        url += domain +"/"+ uploadService.uploadFile(file, spaceFile) + " ";
                        url += uploadService.uploadFile(file, spaceFile) + " ";
                        System.out.println("回调地址：" + url);
                    } catch (Exception e) {
                        return CommonResult.failed("图片大于5M，请重新上传！");
                    }
                }
            }
        }
        return CommonResult.successData(url);
    }
}
